<template>
  <div class="register-index">
    <el-card class="box-card">
      <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-position="left"
          label-width="80px"
          class="form-css"
      >
        <el-form-item label="手机号" prop="phone" class="el-avatar--small">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="passwd">
          <el-input
              type="password"
              v-model="ruleForm.passwd"
              autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password">
          <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="ruleForm.gender">
            <el-radio :label="'M'">男</el-radio>
            <el-radio :label="'F'">女</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="批次" prop="teamId">
          <el-select v-model="ruleForm.teamId" placeholder="请选择批次">
            <el-option
                v-for="item in batches"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div class="btnGroup">
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="goBack">返回</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { registerUser, getTeams } from "@/api/register";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.passwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        phone: "",
        passwd: "",
        password: "",
        name: "",
        gender: "",
        teamId: "",
      },
      batches: [],
      rules: {
        phone: [
          { required: true, message: "用户名不能为空！", trigger: "blur" },
        ],
        passwd: [{ required: true, validator: validatePass, trigger: "blur" }],
        password: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getTeam();
  },
  methods: {
    getTeam() {
      getTeams()
          .then((response) => {
            this.batches = response.data;
          })
          .catch((error) => {
            console.error("获取批次数据失败:", error);
          });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const formData = {
            phone: this.ruleForm.phone,
            passwd: this.ruleForm.passwd,
            name: this.ruleForm.name,
            gender: this.ruleForm.gender,
            teamId: this.ruleForm.teamId,
          };

          registerUser(formData)
              .then((response) => {
                console.log("注册成功:", response.data);
                alert("注册成功！");
              })
              .catch((error) => {
                console.error("注册失败:", error);
                alert("注册失败，请重试！");
              });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.register-index {
  background: url("../assets/bg/bg1.png");
  background-size: 102% 130%;
  overflow: hidden;
}
.box-card {
  background: url("../assets/bg/reg.png");
  background-size: 100% 100%;
  width: 450px;
  height: 700px;
  margin: 100px auto;
  overflow: hidden;
  padding-top: 10px;
  line-height: 40px;
  box-shadow: 0 4px 15px rgba(255, 165, 100, 0.7);
}
.form-css {
  margin-top: 100px;
}
.el-form-item {
  width: 380px;
  height: 20px;
  margin-left: 10px;
}
::v-deep .el-input__inner {
  border-radius: 0px;
  border-top-width: 0px;
  border-left-width: 0px;
  border-right-width: 0px;
  border-bottom-width: 1px;
}
.login-from {
  margin: auto auto;
}
</style>
